<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<div class="pro_list fr">
   <div class="title"  >
   	 <span class="titletxt">您现在的位置：</span>
     <ul> 
       <li class="root">会员中心</li>  
       <li>订单管理</li>      
       <li>订单确认</li>            
     </ul>
   </div>
   <c:if test="${not empty ret }">
   	<c:if test="${ret.code ne '200'}">
   		<script type="text/javascript">alertError('${ret.result}');</script>
   		<div id=messager class="messager" >
	         <div class="contentmsg">
	             <h5>提示信息～！</h5>
	             <ul>
	               <li>${ret.result}</li>
	             </ul>
	        </div>
	     <div class="closer" onClick="showHideLayer('messager');" title="关闭"></div>
	    </div>
   	</c:if>
   </c:if>
   <div id="p" class="easyui-panel" title="订单确认"  style="padding:10px; ">
   <form action="<%=request.getContextPath() %>/trade/generateOrder.s" name="orderForm" id="orderForm" method="post">
       <input type="hidden"  id="codes" name="codes" value="${ret.data.codes}"/>
       <div class="easyui-tabs" title="订单确认" data-options="tools:'#tab-tools',plain:true" >
            <div  title="确认商品信息" >
               <table id="tableGrid"  class="easyui-datagrid" style="height: 400px;">   </table>
        	</div>
       </div>
       <!-- 确认收货信息 -->
       <div class="easyui-tabs" data-options="tools:'#tab-tools',plain:true" style="height:200px;margin-top: 10px;">
            <div  title="确认收货信息" >
             <c:if test="${not empty ret.data.addressList }">
            	<c:forEach var="address" items="${ret.data.addressList}" begin="0" end="0">
            	<input type="hidden" value="${address.buyerAddressId}" name="buyerAddressId" id="buyerAddressId"/>
            	<table   border="0" cellspacing="0" cellpadding="0" style="margin: 10px;border-top: 1px solid #ddd;">
                      <tr>
                        <td class="table_td">收货地址  </td>
                        <td class="juleft">
                      		<p>${address.address } <a href="<c:url value="/views/jsp/center/address/c_user_shipping_address.jsp" />" class="easyui-linkbutton" style="width:100px; margin-top:20px; display:inline; float:right;">更换默认地址</a></p>
                        </td>
                      </tr>
                      <tr>
                        <td class="table_td">收货联系人</td>
                        <td class="juleft"  >
                        	<p>${address.consignee }</p>
                        </td>
                      </tr>
                      <tr>
                        <td class="table_td">收货人联系电话</td>
                        <td class="juleft">
                       		 ${address.phone }
                        </td>
                      </tr>
                      
              </table>
            	</c:forEach>
            </c:if>
            <c:if test="${empty ret.data.addressList }">
            	<table   border="0" style="margin-left: 20px;background-color: #f7f7f7;"  >      
		             <tbody >     
		              <tr  style="height: 20px;">
		              <td style="text-align: left;font-size: 18px;font-weight: 700;"> 
		                                                        客户资料不全    您还没有 收货地址！
		                             <a href="<c:url value="/views/jsp/center/address/c_user_shipping_address.jsp" />" class="easyui-linkbutton" style="width:100px; margin-top:20px; display:inline; float:right;">马上去添加</a>                     
		              </td>
		              </tr>
		             </tbody>               
	             </table>    
            </c:if>
            	
        	</div>
       </div>
       
       <!-- 确认开票信息 -->
       <div class="easyui-tabs" data-options="tools:'#tab-tools',plain:true" style="height:310px;margin-top: 10px;">
            <div  title="确认开票信息" >
            	<table   border="0" cellspacing="0" cellpadding="0" style="margin: 10px;border-top: 1px solid #ddd;">
                       <tr>
                        <td class="table_td">开票开户银行</td>
                        <td class="juleft">${ret.data.emCBuyer.invbankname} </td>
                      </tr>
                      <tr>
                        <td class="table_td">开票电话</td>
                        <td class="juleft"> ${ret.data.emCBuyer.invbankname}</td>
                      </tr>
                      <tr>
                        <td class="table_td">开票开户账号</td>
                        <td class="juleft">${ret.data.emCBuyer.invaccno} </td>
                      </tr>
                      <tr>
                        <td class="table_td">开票税号</td>
                        <td class="juleft"> ${ret.data.emCBuyer.invtaxno}</td>
                      </tr>
                      <tr>
                        <td class="table_td">开票单位</td>
                        <td class="juleft"> ${ret.data.emCBuyer.invaddress}</td>
                      </tr>
                      <tr>
                        <td class="table_td">开票地址</td>
                        <td class="juleft"> ${ret.data.emCBuyer.invcustomname}</td>
                      </tr>
              </table>
        	</div>
       </div>
            	
             <div style="float:right; margin-right:20px; display:inline;">
                <br/>
                <input type="checkbox" id="check_isInvoice" title="支付金额*1.09"/>
                是否开票
                <input type="hidden" name="isInvoice" id="isInvoice" value="0">
                <br/>
             	需要支付总金额：<span id="needPayMoney" style="font-size:24px; color:red; font-family:"微软雅黑"; font-weight:bold;"></span>
                 <br>
                 <a href="javascript:void(0)" onclick="subShoppingCartFrom()" class="easyui-linkbutton" style="width:100px; margin-top:20px; display:inline; float:right;">提 交 订 单</a>
             </div>
         </form> 	
    </div>
</div>
<script>
var productsTable;
var needPayMoney;
$(function(){
	initProducts();
     $("#check_isInvoice").click(function(){
        if($(this).prop("checked")==true){
            $("#isInvoice").val(1);
        /*     var money=needPayMoney*1.09;
            $("#needPayMoney").html(money.toFixed(2)); */
        }else{
            $("#isInvoice").val(0);
         //   $("#needPayMoney").html(needPayMoney);
        }
    }); 
	//获取订单
});
function initProducts(){
    needPayMoney=<c:out value="${ret.data.sumMoney}"/>
    $("#needPayMoney").html(needPayMoney);
	productsTable = $('#tableGrid').pDataGrid({
		 columns:[[
	          	  {field:'productCode',title:'编码',align:'center',sortable:true},
	              {field:'chsName',title:'品名',align:'center',sortable:true},
	              {field:'productSpec',title:'规格',align:'center',sortable:true},
	              {field:'producingName',title:'生产厂家',align:'center',sortable:true},
	              {field:'authorizeNumber',title:'批准文号',align:'center',sortable:true},
	              {field:'nowPrice',title:'价格类型',align:'center',sortable:true},
	              {field:'salePrice',title:'价格(元)',align:'center',sortable:true},
	              {field:'minPackingNumber',title:'中包装',align:'center',sortable:true},
	              {field:'packingAmount',title:'整件数',align:'center',sortable:true},
	              {field:'amount',title:'购买数量',align:'center',sortable:true},
	              {field:'sumMoney',title:'汇总',align:'center',sortable:true}
		          ]],
            data:<c:out value=" ${ret.data.shoppingCartJson}" escapeXml="false"/>,
            pagination:false,
		    singleSelect:false,
		    rownumbers:false,
			nowrap: true
	}); 
}
var canSub = true;
function subShoppingCartFrom(){
    var codes=$("#codes").val();
    var isInvoice=$("#isInvoice").val();
    var buyerAddressId=$("#buyerAddressId").val();
    if(canSub){
    	canSub = false;
    	$.pmsAjax({
            url:'<c:url value="/trade/generateOrderAjax.s"/>',
            data:{'codes':codes,'isInvoice':isInvoice,"buyerAddressId":buyerAddressId},
            success:function(ret){
                if(ret.code=="200"){
                   var orderCode=ret.data.orderCode;
                   var money=ret.data.money;
                   payOrder(orderCode,money);
                   canSub = true;
                }else{
                    layer.msg(ret.result,2, {type: 8,shade: [0.2, '#ccc']});
                    canSub = true;
                }
            }
        });
    }else{
    	layer.msg("订单正在提交,请稍后");
    }
    
}
function payOrder(code,money){

    //检查支付协议是否签署
    $.pmsAjax({
        url:'<c:url value="/user/checkPayCompact.s"/>',
        data:{},
        success:function(data){
            if(data.code=="200"){
                var param = {"code":code,"money":money,"skip":"1"};
                $.pmsDialog({
                    id:'win_order_pay',
                    title:'订单支付方式选择',
                    href:'<c:url value="/views/jsp/center/trade/pay/pay_list.jsp"/>',
                    width:1000,
                    height:600,
                    params:param
                });
            }else{
                //弹出 签署协议
                //弹出合同签订
                $.pmsDialog({
                    id:'pay_compact_win',
                    title:'在线支付协议',
                    method:'post',
                    href:'<c:url value="/views/jsp/center/compact/pay_compact_detail.jsp"/>',
                    width:800,
                    height:500,
                    buttons: '#dlg-buttons'
                });
            }
        }
    });

    //$("#orderCode").val(orderCode);
    //document.forms["payOrderForm"].submit();

}
</script>